package org.dromara.third.convert.roadWork.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.RoadWork;
import org.dromara.system.mapper.RoadWorkMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.roadWork.dto.RoadWorkDTO;
import org.dromara.third.convert.roadWork.validator.RoadWorkValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertRoadWorkController {

    @Autowired
    private RoadWorkMapper roadWorkMapper;

    @PostMapping("/roadWork")
    @Transactional
    public R<Void> addRoadWork(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        RoadWorkDTO roadWorkDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            roadWorkDTO = JSONObject.parseObject(decrypt, RoadWorkDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, roadWorkDTO, new RoadWorkValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        RoadWork roadWork = assembleRoadWorkData(orgCode, roadWorkDTO);
        int count = roadWorkMapper.countWithOutDelFlag(roadWork.getId());
        if (count == 0) {
            roadWorkMapper.insert(roadWork);
        } else {
            roadWorkMapper.updateWithOutDelFlag(roadWork);
        }
        return R.ok();
    }

    private RoadWork assembleRoadWorkData(String orgCode, RoadWorkDTO dto) {
        RoadWork roadWork = new RoadWork();
        try {
            roadWork.setId(dto.getZyid());
            roadWork.setZyzbh(dto.getZyzbh());
            roadWork.setSqdw(dto.getSqdw());
            roadWork.setZysqsj(DateUtils.parseDate(dto.getZysqsj()));
            roadWork.setSqr(dto.getSqr());
            roadWork.setDlyy(dto.getDlyy());
            roadWork.setZynr(dto.getZynr());
            roadWork.setZywz(dto.getZywz());
            roadWork.setXgdwbm(dto.getXgdwbm());
            roadWork.setZydwlx(dto.getZydwlx());
            roadWork.setZydwmc(dto.getZydwmc());
            roadWork.setZyfzr(dto.getZyfzr());
            roadWork.setZysskssj(DateUtils.parseDate(dto.getZysskssj()));
            roadWork.setZyssjssj(DateUtils.parseDate(dto.getZyssjssj()));
            roadWork.setDlddsyt(dto.getDlddsyt());
            roadWork.setZyfwFs(dto.getZyfwFs());
            roadWork.setBzrmc(dto.getBzrmc());
            roadWork.setBzsj(DateUtils.parseDate(dto.getBzsj()));
            roadWork.setSfsjqttszy(dto.getSfsjqttszy());
            roadWork.setSjqttszyzbh(dto.getSjqttszyzbh());
            roadWork.setFxbs(dto.getFxbs());
            roadWork.setAqjdr(dto.getAqjdr());
            roadWork.setJsjdr(dto.getJsjdr());
            roadWork.setJhr(dto.getJhr());
            roadWork.setDqzt(dto.getDqzt());
            roadWork.setLongitude(dto.getLongitude());
            roadWork.setLatitude(dto.getLatitude());
            roadWork.setStartTime(DateUtils.parseDate(dto.getStartTime()));
            roadWork.setEndTime(DateUtils.parseDate(dto.getEndTime()));
            roadWork.setSpgbbm(dto.getSpgbbm());
            roadWork.setFhyp(dto.getFhyp());
            roadWork.setAttachment(dto.getAttachment());
            roadWork.setDelFlag(Long.parseLong(dto.getDelFlag()));
            roadWork.setCreateDept(Long.parseLong(dto.getSsbm()));
            roadWork.setCreateBy(Long.parseLong(dto.getCreateBy()));
            roadWork.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            roadWork.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            roadWork.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            roadWork.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装断路作业信息异常");
        }
        return roadWork;
    }
}
